/**
 * @file
 * <pre> CPE 464 Winter 2012
 * -------------------
 *
 *  Header to "progrma header"
 *
 *  Last Modified: Sun May 13 02:53:28 PM PDT 2012</pre>
 *  @author Matthew Tondreau
 */

#ifndef _SERVER_H_
#define _SERVER_H_

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <string>
#include <map>
#include <vector>

#include "utility.h"
using std::map;
using std::string;
using std::vector;

#define MAX_CONNECTIONS 20
#define MAX_RECV 512
#define MAX_SELECT_SEC 2
#define MAX_SELECT_MSEC 0
#define MYPORT "5577"
#define LOCK() { pthread_mutex_lock( &mutex ); }
#define UNLOCK() { pthread_mutex_unlock( &mutex ); }

enum STATES {
   ERROR,
   SETUP,
   LISTEN,
   TIMEOUT,
   EXIT
};


typedef struct {
   int uidsk[MAX_CONNECTIONS];
   vector<int> vSock;
} data_t;

typedef struct {
   data_t* d;
   int sk;
   int id;
} thread_param;

extern jmp_buf env;
extern pthread_mutex_t mutex;
extern bool doexit;

int setup_server();
void sigint_handler( int i ); 
   
/** select on all open sockets 
 * if main socket goto login 
 * else if other socket goto verify
 * else goto timeout
 */
STATES tcp_listen(int sk, data_t &data, vector<pthread_t> &threds);
void* handle_client( void * v );
int logout_user( int sk, int id, data_t &data );
                       
#endif
